/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package libraries;

// @author Santhosh Kumar T - santhosh@in.fiorano.com 
// Usage: new FileAutoCompleter(yourJTextField);


import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.regex.Pattern;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;



public class FileAutoCompleter extends AutoCompleter{
   
    private ArrayList<String> autoCompletionList;
    private ArrayList<JTextComponent> components;
    private int varNumber;
            
    public FileAutoCompleter(ArrayList<JTextComponent> components, ArrayList<String> autoCompletionList, int varNumber){ 
        super(components.get(varNumber)); 
        this.components = components;        
        this.autoCompletionList = autoCompletionList;
        this.varNumber = varNumber;
    }
    
    protected boolean updateListData(){ 
        String prefix = textComp.getText(); 
                
        ArrayList<String> filteredList = getMatches(autoCompletionList, ".*"+prefix+".*");   // Search coincidences in all list.
        //System.out.println("Filtered List: "+filteredList);
 
        String[] files = filteredList.toArray(new String[filteredList.size()]);
        
        if(files == null){ 
            list.setListData(new String[0]); 
            return true; 
        } else{ 
            if(files.length==1 && files[0].equalsIgnoreCase(prefix)) 
                list.setListData(new String[0]); 
            else 
                list.setListData(files); 
            return true; 
        } 
    } 
 
    protected void acceptedListItem(String selected){ 
        if(selected==null) 
            return; 
        
        String value = textComp.getText(); 
        int prefixlen = textComp.getText().length(); 
        System.out.println("prefixlen: "+prefixlen);
        
        
            
        //textComp.setText(null);  
        //textComp.getDocument().insertString(0, selected, null);  // Adds the remaining string to complete the word.
        int pos = (autoCompletionList.indexOf(selected)-varNumber);
        //System.out.println("Position: "+pos);
        for(int i=0;i<components.size();i++){
            this.components.get(i).setText(autoCompletionList.get(pos+i));
        }
        //textComp.setFocusable(false);
        //textComp.getDocument().insertString(textComp.getText().length(), selected.substring(prefixlen), null);  // Adds the remaining string to complete the word.
            
        
    }
    
    public ArrayList<String> getMatches(ArrayList<String> list, String regex) {  
        ListIterator<String> li = list.listIterator();   
        ArrayList<String> indexes = new ArrayList<String>();  
             
        while(li.hasNext()) {    
            //System.out.println("Regex: "+regex);            
            String element = null; 
            for(int i=0; i<this.components.size(); i++){ // Jump other variables.                
                if(i==this.varNumber){element = li.next();}
                else{li.next();}                
            } 
            //System.out.println("Element: "+element);
            if(Pattern.matches(regex.toLowerCase(), element.toLowerCase())) {  
                indexes.add(element);    
            }  
            
        }  
        return indexes;
    }
}